<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-header">
                <h3 class="box-title">日志管理</h3>
                <div class="box-tools">
                    <div class="input-group input-group-sm" style="width: 350px;">
                        <input type="text" name="table_search" id="searchNameId"
                               class="form-control pull-right" placeholder="用户名">

                        <div class="input-group-btn">
                            <button type="button" class="btn btn-default btn-search">
                                <i class="fa fa-search"></i>
                            </button>
                            <button type="button" class="btn btn-default btn-delete">删除</button>
                        </div>
                    </div>
                </div>
            </div>
            <!-- /.box-header -->
            <div class="box-body table-responsive no-padding">
                <table class="table table-hover">
                    <thead>
                    <tr>
                        <th><input type="checkbox" id="checkAll">全选</th>
                        <th>用户名</th>
                        <th>操作</th>
                        <th>请求方法</th>
                        <th>请求参数</th>
                        <th>IP</th>
                        <th>执行时长</th>
                    </tr>
                    </thead>
                    <tbody id="tbodyId">
                    <tr>
                        <td colspan="7">数据正在加载中...</td>
                    </tr>
                    </tbody>
                </table>
            </div>
            <div id="pageId" class="box-footer clearfix" dm="100">

            </div>
            <!-- /.box-body -->
        </div>
        <!-- /.box -->
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $("#pageId").load("doPageUI", function () {
            doGetObjects();
        });
        $(".input-group-btn").on("click", ".btn-search", doQueryObjects);
        $(".input-group-btn").on("click", ".btn-delete", doDeleteObjects);
        $("#checkAll").change(doChangeTBodyCheckBoxState);
        $("#tbodyId").on("change",".cBox",doChangeTHeadCheckBoxState);
    });


    function doHandleQueryResponseResult(result) { //JsonResult
        if (result.state == 1) { //ok
            doSetTableBodyRows(result.data.records); //将数据呈现在页面上
            doSetPagination(result.data);
        } else {
            doSetErrorMsg(result.msg);
        }
    }

    function doSetErrorMsg(msg) {
        var tBody = $("#tbodyId");
        tBody.empty();
        var td = "<td colspan='7'>" + msg + "</td>"
        tBody.append(td);
        var data = {"rowCount": 0, "pageCount": 0, "pageCurrent": 1};
        doSetPagination(data);
    }

    function doSetTableBodyRows(records) {
        var tBody = $("#tbodyId");
        tBody.empty();
        for (var i in records) {
            var tr = $("<tr></tr>");
            var tds = doCreateTds(records[i]);
            tr.append(tds);
            tBody.append(tr);
        }
    }

    function doCreateTds(data) {
        var tds = "<td><input type='checkbox' class='cBox' name='cItem' value='" + data.id + "'></td>" +
            "<td>" + data.username + "</td>" +
            "<td>" + data.operation + "</td>" +
            "<td>" + data.method + "</td>" +
            "<td>" + data.params + "</td>" +
            "<td>" + data.ip + "</td>" +
            "<td>" + data.time + "</td>";
        return tds;
    }


    function doQueryObjects() {
        debugger;
        $("#pageId").data("pageCurrent", 1);
        doGetObjects();
    }

    function doGetObjects() {
        debugger;
        $("#checkAll").prop("checked",false);
        var url = "log/doFindPageObjects"
        var pageCurrent = $("#pageId").data("pageCurrent");
        if (!pageCurrent) pageCurrent = 1;
        var params = {"pageCurrent": pageCurrent};
        var username = $("#searchNameId").val();
        if (username) params.username = username;
        $.getJSON(url, params, function (result) {
                doHandleQueryResponseResult(result);
            }
        );
    }

    function doDeleteObjects() {
        var ids = doGetCheckedIds();
        if (ids.length == 0) {
            alert("至少选择一个");
            return;
        }
        if (!confirm("您确定删除吗")) {
            return;
        }
        var url = "log/doDeleteObjects";
        var params = {"ids": ids.toString()};
        console.log(params);
        $.post(url, params, function (result) {
            if (result.state == 1) {
                alert(result.msg);
                doRefreshAfterDeleteOK();
            } else {
                alert(result.msg);
            }
        });
        // $.ajax({
        //     url:url,
        //     type:"delete",
        //     success:result=>{
        //         if (result.state == 1) {
        //             alert(result.msg);
        //             doRefreshAfterDeleteOK();
        //         } else {
        //             alert(result.msg);
        //         }
        //     }
        // })
    }

    function doGetCheckedIds() {
        var array = []; //new Array();
        $("#tbodyId input[type=checkbox]").each(function () {
            if ($(this).prop("checked")) {
                array.push($(this).val());
            }
        });
        return array;
    }

    function doChangeTBodyCheckBoxState() {
        var flag = $(this).prop("checked");//true or false
        $("#tbodyId input[type='checkbox']").prop("checked", flag);
    }

    function doChangeTHeadCheckBoxState() {
        let flag=true;//默认值必须为true
        $("#tbodyId input:checkbox")
            .each(function(){
                flag=flag&&$(this).prop("checked")
            })
        //2.修改thead中checkbox的值
        $("#checkAll").prop("checked",flag);
    }

    function doRefreshAfterDeleteOK() {
        var pageCount = $("#pageId").data("pageCount");
        var pageCurrent = $("#pageId").data("pageCurrent");
        var checked = $("#checkAll").prop("checked");
        if (pageCurrent == pageCount && checked && pageCurrent > 1) {
            pageCurrent--;
            $("#pageId").data("pageCurrent", pageCurrent);
        }
        doGetObjects();
    }

</script>






